$(document).ready(function () {
	//时间格式化
	function dateFtt(fmt,date) {
		var o = {
			"M+" : date.getMonth()+1,                 //月份
			"d+" : date.getDate(),                    //日
			"h+" : date.getHours(),                   //小时
			"m+" : date.getMinutes(),                 //分
			"s+" : date.getSeconds(),                 //秒
			"q+" : Math.floor((date.getMonth()+3)/3), //季度
			"S"  : date.getMilliseconds()             //毫秒
		};
		if(/(y+)/.test(fmt))
			fmt=fmt.replace(RegExp.$1, (date.getFullYear()+"").substr(4 - RegExp.$1.length));
		for(var k in o)
			if(new RegExp("("+ k +")").test(fmt))
				fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
		return fmt;
	}

	//获取地址栏参数
	function getUrlParms(name){
		var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
		var r = window.location.search.substr(1).match(reg);
		if( r != null )
			return unescape(r[2]);
		return null;
	}

	//获取充值详情
	get_recharge_info();
	function get_recharge_info() {
		$.ajax({
			url: "/index/rechargeDetail",
			type: "POST",
			dataType: "json",
			data: {
				recharge_id: getUrlParms('recharge_id')
			},
			success: function (data) {
				if( data.code == '1' ){
					fill_data_page(data.data);//填充数据到页面
				}else {

				}
			},
			error: function (err) {
				console.log(err);
			}
		})
	}

	//获取积分类型
	function get_score_type(type) {
		var score_type = '';//积分类型 1：普通  2：白金  3：钻石  5:黄金  6：口腔
		if( type == '1' ){
			score_type = '普通积分';
		}
		if( type == '2' ){
			score_type = '白金积分';
		}
		if( type == '3' ){
			score_type = '钻石积分';
		}
		if( type == '5' ){
			score_type = '黄金积分';
		}
		if( type == '6' ){
			score_type = '口腔积分';
		}
		return score_type;
	}

	//获取审核状态
	function get_is_check(check) {
		var is_check = '';//审核状态 -1:已取消 0:未审核 1:已审核
		var is_check_active = '';
		if( check == '-1' ){
			is_check = '已取消';
			is_check_active = '';
		}
		if( check == '0' ){
			is_check = '未审核';
			is_check_active = 'active';
		}
		if( check == '1' ){
			is_check = '已审核';
			is_check_active = '';
		}

		return {
			is_check:is_check,
			is_check_active:is_check_active
		};
	}

	//填充数据到页面
	function fill_data_page(data) {
		var check_at = '';
		if( data.check_at == '0' ){
			check_at = '--'
		}else {
			check_at = dateFtt('yyyy-MM-dd hh:mm',new Date(data.add_at*1000));
		}

		var html = '';
		html += '<div class="clearfix">';
		html += '<div class="pull-left">序号: <span>'+ data.recharge_id +'</span></div>';
		html += '</div>';
		html += '<div class="clearfix">';
		html += '<div class="pull-left">充值人</div>';
		html += '<div class="pull-right">'+ data.user_name +'</div>';
		html += '</div>';
		html += '<div class="clearfix">';
		html += '<div class="pull-left">手机号</div>';
		html += '<div class="pull-right">'+ data.user_mobile +'</div>';
		html += '</div>';
		html += '<div class="clearfix">';
		html += '<div class="pull-left">充值积分</div>';
		html += '<div class="pull-right">'+ get_score_type(data.score_type) +'</div>';
		html += '</div>';
		html += '<div class="clearfix">';
		html += '<div class="pull-left">充值金额</div>';
		html += '<div class="pull-right">'+ data.recharge_money +'</div>';
		html += '</div>';
		html += '<div class="clearfix">';
		html += '<div class="pull-left">状态</div>';
		html += '<div id="is_check" class="pull-right '+ get_is_check(data.is_check).is_check_active +'">'+ get_is_check(data.is_check).is_check +'</div>';
		html += '</div>';
		html += '<div class="clearfix">';
		html += '<div class="pull-left">备注</div>';
		html += '<div class="pull-right">'+ data.remark +'</div>';
		html += '</div>';
		html += '<div class="clearfix">';
		html += '<div class="pull-left">充值时间</div>';
		html += '<div class="pull-right">'+ dateFtt('yyyy-MM-dd hh:mm',new Date(data.add_at*1000)) +'</div>';
		html += '</div>';
		html += '<div class="clearfix">';
		html += '<div class="pull-left">审核时间</div>';
		html += '<div class="pull-right">'+ check_at +'</div>';
		html += '</div>';

		$('#recharge_info').html( html );
		$('.btn_group').addClass( get_is_check(data.is_check).is_check_active );
	}
	
	/* **************************************************
	*                   审核
	* ****************************************************/
	//取消审核
	cancel_check();
	function cancel_check() {
		$('#cancel_check').click(function () {
			var check_tip = $('#check_tip');
			check_tip.modal({keyboards:false});
			$('#sure').unbind('click');
			$('#sure').click(function () {
				cancel_check_ajax();//通过审核 ajax
			});
		});
	}

	//通过审核 ajax
	function cancel_check_ajax(){
		$.ajax({
			url: "/index/delRecharge",
			type: "POST",
			dataType: "json",
			data: {
				recharge_id: getUrlParms('recharge_id')
			},
			success: function (data) {
				if( data.code == '1' ){

					$('#check_tip').modal('hide');//hide  modal
					$('.btn_group').removeClass('active');//hide  btn
					get_recharge_info();//获取充值详情

				}else {
					console.log(data.msg);
				}
			},
			error: function (err) {
				console.log(err);
			}
		})
	}

	//通过审核
	pass_check();
	function pass_check() {
		$('#pass_check').click(function () {
			var check_tip = $('#check_tip');
			check_tip.modal({keyboards:false});
			$('#sure').unbind('click');
			$('#sure').click(function () {
				pass_check_ajax();//通过审核 ajax
			});
		});
	}

	//通过审核 ajax
	function pass_check_ajax(){
		$.ajax({
				url: "/index/checkRecharge",
				type: "POST",
				dataType: "json",
				data: {
					recharge_id: getUrlParms('recharge_id')
				},
				success: function (data) {
					if( data.code == '1' ){

						$('#check_tip').modal('hide');//hide  modal
						$('.btn_group').removeClass('active');//hide  btn
						get_recharge_info();//获取充值详情

					}else {
						console.log(data.msg);
					}
				},
				error: function (err) {
					console.log(err);
				}
			})
	}

	/* *****************************************************
	*                返回上一页
	* ******************************************************/
	go_back();
	function go_back() {
		$('#back').click(function () {
			history.go(-1);
		});
	}
});